
use "$data/APcontractlaborlong_final1.dta", clear
*    global output "/Users/rchaure1/Dropbox/APcontract_reducedform (1)/output/outputJan2024/"

	*keep if major_new==1 & statusunit==1
	*keep if major_extended==1 & statusunit==1
	*keep if major_all==1 & statusunit==1

	qui tabulate year, generate (yr_)

	keep if statusunit==1
 
	generate treat00 = treat*yr_1
	generate treat01 = treat*yr_2
	generate treat02 = treat*yr_3
	generate treat03 = treat*yr_4
	generate treat04 = treat*yr_5
	generate treat05 = treat*yr_6
	generate treat06 = treat*yr_7
	generate treat07 = treat*yr_8
	generate treat08 = treat*yr_9
	generate treat09 = treat*yr_10
	generate treat10 = treat*yr_11
	generate treat11 = treat*yr_12
	generate treat12 = treat*yr_13
	generate treat13 = treat*yr_14
	generate treat14 = treat*yr_15
	generate treat15 = treat*yr_16	

	
	generate zero = 0
	label var treat00 "1999-00"
	label var treat01 "2000-01"
	label var treat02 "2001-02"
	label var treat03 "2002-03"
	label var treat04 "2003-04"
	label var treat05 "2004-05"
	label var treat06 "2005-06"
	label var treat07 "2006-07"
	label var treat08 "2007-08"
	label var treat09 "2008-09"
	label var treat10 "2009-10"
	label var treat11 "2010-11"
	label var treat12 "2011-12"
	label var treat13 "2012-13"
	label var treat14 "2013-14"
	label var treat15 "2014-15"
	label var zero "2002-03"

	gen regularwages_total = regularsalaries/regular_totmandays
	gen lrwages_total = ln(regularwages_total)
	gen realregularwages = regularwages_total *(wpiop2011/wpiop)
	gen lrealregularwages = ln( realregularwages) 
	gen regularwagespaid_total = regularsalaries/regularmandayspaid
	gen lrwagespaid_total = ln(regularwagespaid_total)
	gen realregularwagespaid_total = regularwagespaid_total *(wpiop2011/wpiop)
	gen lrealregularwagespaid_total = ln(realregularwagespaid_total)

	gen price = egen_exfactoryvalop1/egen_qtysold1
	gen lprice = ln(price)

	egen stateyear = group(state year)
	egen stateind4digit = group(state ind4digit)



	gen contracttoregular_workers = contract_workers/regular_workers
	gen contracttoregular_mandays = contract_mandays/regularmandays 
	gen logcontracttoregular_workers = ln(contracttoregular_workers+1)
	gen logcontracttoregular_mandays = ln(contracttoregular_mandays+1)

	gen cw = exp(lncw)
	gen w = exp(lnw)
	gen rw = exp(lnrw)
	gen cwratio_new = cw/w

	capture drop lncw_w 
	capture drop lnrw_w 

	foreach v of varlist  lcm_man cratio_man cwratio lcw lcm_total lcm_nonman contract_workers contract_mandays contract_totmandays cratio_total cratio_nonman workers regular_workers regular_totmandays regularmandays regularwages_total allrevenue lrw lw lrwages_total lrm_man lrm_nonman lwm_total lrm_total lnrw lncw lnw cwratio_new cw rw w {
	winsor2 `v', cuts(1 99)
	}

	gen age = year - yearinitialprod
	replace age = . if year< yearinitialprod
	replace age = . if yearinitialprod==0
	replace age = . if yearinitialprod<1900
	
	label variable lncw_w "Log Contract Workers"
	label variable cwratio_new_w "Frac. Contract Workers"
	label variable lnrw_w "Log Regular Workers"
	label variable lcm_total_w "Log Contract Worker-Days"
	label variable lrm_total_w "Log Regular Worker-Days"
	label variable cratio_total_w "Frac. Contract Worker-Days" 
	label variable posttreat "Post X Treat"


gen ind2digit = int(ind3digit/10)

global fe "factory year ind2digit##i.year state##c.year"
global cl "state"
 


*****************************
* EVENT STUDY GRAPHS - Figure A3
*****************************
	est clear
	local i = 1
	foreach v of varlist lncw_w lcm_total_w lnrw_w lrm_total_w cwratio_new_w cratio_total_w {
	qui reghdfe `v' treat01 treat02 zero treat04 treat05 treat06 treat07 treat08 treat09   ///
			if year<=2009 [pw=weight], cl(${cl}) absorb(${fe})
	estimates store model`v'  
	eststo c`i'
	noi di "Done with `v'"
	
	local i=`i'+1
	qui test treat01 = treat02 = 0
	}

coefplot (c1, recast(connected) lcolor(black) mcolor(black) msymbol(square) ///
		  ciopts(recast(rarea) fcolor(gs12) lcolor(gs12))) ///
		 (c2, mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ylabel(, format(%02.1f)) ///
					graphregion(color(white)) lcolor(emidblue) ///
					legend(pos(6) rows(1) label(2 "Log Contract Workers") label(4 "Log Contract Worker-Days"))
graph export "${output}/asi/FigureA3a.png", as(png) name("Graph") replace

coefplot (c3, recast(connected) lcolor(black) mcolor(black) msymbol(square) ///
		  ciopts(recast(rarea) fcolor(gs12) lcolor(gs12))) ///
		 (c4, mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ylabel(, format(%02.1f)) ///
					graphregion(color(white)) lcolor(emidblue) ///
					legend(pos(6) rows(1) label(2 "Log Regular Workers") label(4 "Log Regular Worker-Days"))
graph export "${output}/asi/FigureA3b.png", as(png) name("Graph") replace

coefplot (c5, recast(connected) lcolor(black) mcolor(black) msymbol(square) ///
		  ciopts(recast(rarea) fcolor(gs12) lcolor(gs12))) ///
		 (c6, mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ylabel(, format(%02.1f)) ///
					graphregion(color(white)) lcolor(emidblue) ///
					legend(pos(6) rows(1) label(2 "Frac. Contract Workers") label(4 "Frac. Contract Worker-Days"))
graph export "${output}/asi/FigureA3c.png", as(png) name("Graph") replace






